<template>
  <StaticTable :loading="loading" :tableList="data">
    <el-table-column type="selection" width="55" />
    <el-table-column
      label="规则编号"
      width="200"
      :show-overflow-tooltip="true"
      prop="innerVersionNo"
      align="center"
    />
    <el-table-column label="名称" :show-overflow-tooltip="true" prop="versionName" align="center" />
    <el-table-column
      label="版本"
      :show-overflow-tooltip="true"
      prop="versionNo"
      align="center"
      sortable
    />
    <el-table-column label="操作" align="center" fixed="right" width="100">
      <template #default="{ row }">
        <el-button link type="primary" @click="pushApk(row, 'detail')"> 版本推送 </el-button>
      </template>
    </el-table-column>
  </StaticTable>
</template>

<script setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import { deviceApkVList, pushApkVerions } from '@/api/device/index'

const props = defineProps({
  machineType: [String, Number],
  machineId: [String, Number]
})

const loading = ref(false)
const data = ref([])

watchEffect(async () => {
  const { machineId, machineType } = props

  if (machineType && machineId) {
    loading.value = true
    try {
      const r = await deviceApkVList({ machineType, machineId })
      data.value = r?.data ?? []
    } finally {
      loading.value = false
    }
  }
})

// 版本推送
function pushApk(data) {
  ElMessageBox.confirm('确认要推送该版本吗?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    center: false
  }).then(() => {
    pushApkVerions({
      machineId: [props.machineId],
      appVersionNo: data.versionNo
    }).then(() => {
      ElMessage.success('推送成功')
    })
  })
}
</script>
